package com.p2p.base.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Map.Entry;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class CityUtil {

	
	public static void main(String[] args) throws IOException {
		
		String pathnameProvince = "D:\\doc\\province.json"; 
		String pathNameCity = "D:\\doc\\city.json"; 
		String provinceStr = getFileString(pathnameProvince);
        JSONObject o =  JSON.parseObject(provinceStr);
        for(Entry<String, Object> entry:o.entrySet()){
        	System.out.println("replace p2p_region (id,name,pid) values ('"+entry.getKey()+"','"+entry.getValue()+"',null);");
        }
        
        String cityStr = getFileString(pathNameCity);
        JSONObject cityO =  JSON.parseObject(cityStr);
        for(Entry<String, Object> entry:cityO.entrySet()){
        	JSONArray jarray = JSON.parseArray(entry.getValue().toString());
        	
        	System.out.println("replace p2p_region (id,name,pid) values ('"+entry.getKey()+"','"+jarray.getString(0)+"','"+jarray.getString(1)+"');");
        }
        
	}
	
	public static String getFileString(String pathname) throws IOException{
		 File filename = new File(pathname); // 要读取以上路径的input。txt文件  
	        InputStreamReader reader =  null;
			try {
				reader = new InputStreamReader(  
				        new FileInputStream(filename));
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} // 建立一个输入流对象reader  
	        BufferedReader br = new BufferedReader(reader); // 建立一个对象，它把文件内容转成计算机能读懂的语言  
	        String line = "";  
	        StringBuilder sb = new StringBuilder();
	       
	        while ((line =br.readLine())!=null) {  
	        	sb.append(line);
	        }  
	        return sb.toString();
	}
}
